﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using PSDLib.Models;
using PSDManager.SystemSettings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.CreateTask
{
    public partial class FrmAddTalkOutTask : Form
    {
        public dl_talk_out_task_start StartInfo { get; set; }
        public FrmAddTalkOutTask(string type)
        {
            InitializeComponent();

            lblTaskType.Text = $@"创建外出谈话({type})下载任务";

            GetCaseObject();

            GetTalkRoom();
        }

        private void GetCaseObject()
        {
            this.lvwObject.Items.Clear();

            string query = $@"?objectName={txtObjectName.Text.Trim()}&roomName={txtRoomName.Text.Trim()}&unitName={txtUnitName.Text.Trim()}";
            var result = HttpHelper.Get<HResult<List<en_enter_object>>>(AppSettings.Setting.API.GetCaseObjectList + query);


            if (result.success)
            {
                foreach (var obj in result.data)
                {
                    lvwObject.Items.Add(new ListViewItem(new string[] {
                        (lvwObject.Items.Count+1).ToString(),
                        obj.name,
                        obj.room_name,
                        obj.object_unit,
                        obj.entery_time.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                        obj.leave_time == null ? "" : obj.leave_time.Value.ToString("yyyy-MM-dd HH:mm:ss")
                    })).Tag = obj;
                }
            }
        }

        private void GetTalkRoom()
        {

            this.cmbRoom.Items.Clear();

            var result = HttpHelper.Get<HResult<List<rm_room_info>>>(AppSettings.Setting.API.GetFreeRoom);

            if (result.success)
            {
                foreach (var obj in result.data)
                {
                    this.cmbRoom.Items.Add(obj.room_number);
                }
            }

            this.cmbTalkRoom.Items.Clear();

            result = HttpHelper.Get<HResult<List<rm_room_info>>>(AppSettings.Setting.API.GetDefaultRoom);

            if (result.success)
            {
                foreach (var obj in result.data)
                {
                    this.cmbTalkRoom.Items.Add(obj.room_number);
                }
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (lvwObject.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择一个办案对象");
                return;
            }

            if (cmbRoom.SelectedIndex < 0)
            {
                MessageBox.Show("请选择对象留置间");
                return;
            }

            if (cmbTalkRoom.SelectedIndex < 0)
            {
                MessageBox.Show("请选择谈话房间");
                return;
            }

            if ((dtpStartDate.Value - dtpEndDate.Value).TotalSeconds >= 0)
            {
                MessageBox.Show("开始时间不能早于结束时间");
                return;
            }

            this.StartInfo = new dl_talk_out_task_start();
            this.StartInfo.objectId = (lvwObject.SelectedItems[0].Tag as en_enter_object).id;
            this.StartInfo.roomNum = cmbRoom.SelectedItem.ToString();
            this.StartInfo.talkRoomNum = cmbTalkRoom.SelectedItem.ToString();
            this.StartInfo.startTime = this.dtpStartDate.Value;
            this.StartInfo.endTime = this.dtpEndDate.Value;

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            GetCaseObject();
        }
    }

    public class dl_talk_out_task_start
    {
        public string roomNum { get; set; }
        public string talkRoomNum { get; set; }
        public DateTime startTime { get; set; }
        public DateTime endTime { get; set; }
        public string objectId { get; set; }
    }

}
